package cn.stylefeng.guns.modular.order.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.entity.BasicCustomer;
import cn.stylefeng.guns.modular.basic.entity.BasicWarehouse;
import cn.stylefeng.guns.modular.basic.model.params.BasicGoodsParam;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.order.entity.OrderPut;
import cn.stylefeng.guns.modular.order.entity.OrderPutDetailed;
import cn.stylefeng.guns.modular.order.model.params.OrderPutDetailedParam;
import cn.stylefeng.guns.modular.order.model.params.OrderPutParam;
import cn.stylefeng.guns.modular.order.model.params.OrderTransportParam;
import cn.stylefeng.guns.modular.order.model.result.OrderPutDetailedResult;
import cn.stylefeng.guns.modular.order.model.result.OrderPutResult;
import cn.stylefeng.guns.modular.order.model.result.OrderTransportResult;
import cn.stylefeng.guns.modular.order.service.OrderPutDetailedService;
import cn.stylefeng.guns.modular.order.service.OrderPutService;
import cn.stylefeng.guns.modular.order.service.OrderTransportService;
import cn.stylefeng.guns.modular.storage.service.StoragePutSingleService;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.model.result.DictResult;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.guns.sys.modular.system.service.DictService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * 入库订单表控制器
 */
@Controller
@RequestMapping("/orderPut")
public class OrderPutController extends BaseController {

    private String PREFIX = "/order/orderPut";

    @Autowired
    private OrderPutService orderPutService;

    @Autowired
    private OrderPutDetailedService orderPutDetailedService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicCustomerService basicCustomerService;

    @Autowired
    private BasicWarehouseService basicWarehouseService;

    @Autowired
    private DictService dictService;

    @Autowired
    private StoragePutSingleService storagePutSingleService;

    @Autowired
    private OrderTransportService orderTransportService;

    private Lock lock = new ReentrantLock();

    /**
     * 入库单打印页面
     */
    @RequestMapping("toPrint")
    public String toPrint(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	//查询可选择仓库
    	model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择客户
    	model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/orderPutPrint.html";
    }

    /**
     * 批量打印入库单  页面
     */
    @RequestMapping("print")
    public String print() {
        return PREFIX + "/print.html";
    }

    /**
     * 打印查询
     * 入库单信息
     */
    @ResponseBody
    @RequestMapping("/getPrintOrderPut")
    public OrderPutResult getPrintOrderPut(OrderPutParam orderPutParam) {
    	return orderPutService.getPrintOrderPut(orderPutParam);
    }

    /**
     * 打印查询
     * 入库单明细信息
     */
    @ResponseBody
    @RequestMapping("/getPrintOrderPutDetailed")
    public List<OrderPutDetailedResult> getPrintOrderPutDetailed(String  putDetailedNumber) {
    	OrderPutDetailedParam orderPutDetailedParam = new OrderPutDetailedParam();
    	orderPutDetailedParam.setPutDetailedNumber(putDetailedNumber);
    	List<OrderPutDetailedResult> list = orderPutDetailedService.findListBySpec2(orderPutDetailedParam);
        List<OrderPutDetailedResult> list2 = new ArrayList<OrderPutDetailedResult>();
        List<OrderPutDetailedResult> list3 = new ArrayList<OrderPutDetailedResult>();
        for (int i = 0; i < list.size(); i++) {
        	list3.add(list.get(i));
        	if(list3.size()==8) {
        		OrderPutDetailedResult r = new OrderPutDetailedResult();
        		r.setList(list3);
        		list2.add(r);
        		list3 = new ArrayList<OrderPutDetailedResult>();
        	}
		}
        if(list3.size()>0) {
        	OrderPutDetailedResult r = new OrderPutDetailedResult();
    		r.setList(list3);
    		list2.add(r);
        }
    	return list2;
    }

    /**
     * 上传excel
     * 新方法
     * 191111
     */
	@RequestMapping(value = "/importExceNewmethodl" , method = {RequestMethod.POST} )
    @ResponseBody
    public ResponseData importExceNewmethodl(@RequestPart("file") MultipartFile file,  OrderPutParam orderPutParam)  {
		ResponseData data = new ResponseData();
		lock.lock();//获取锁
		try {
			data = orderPutService.getExceldata(file,orderPutParam,data);//处理导入数据
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();//释放锁
		}
        return data;
    }

	/**
	 * 上传excel
	 * 新方法
	 * 191111
	 */
	@RequestMapping(value = "/importExceNewmethodlLY" , method = {RequestMethod.POST} )
	@ResponseBody
	public ResponseData importExceNewmethodlLY(@RequestPart("file") MultipartFile file,  OrderPutParam orderPutParam)  {
		ResponseData data = new ResponseData();
		lock.lock();//获取锁
		try {
			data = orderPutService.getExceldataLY(file,orderPutParam,data);//处理导入数据
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();//释放锁
		}
		return data;
	}

    /**
     * 导出Excel
     */
    @RequestMapping("/exportExcelBySpec")
    public void exportExcelBySpec(HttpServletResponse response, OrderPutParam orderPutParam,String startTime,String endTime) throws IOException {
    	if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
    		orderPutParam.setCreateName("'"+startTime+"' and '"+endTime+"'");
		}
        Workbook wb = orderPutService.exportToExcel(orderPutParam);
        OutputStream output = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(("Inbound Order Information.xlsx").getBytes(), "iso-8859-1"));
        wb.write(output);
        output.close();
    }

    /**
     * 批量提交
     */
    @RequestMapping("/submission")
    @ResponseBody
    public ResponseData submission(@RequestParam("ids") Long[] ids) {
    	ResponseData data = new ResponseData();
		lock.lock();//获取锁
		try {
			//批量生成入库单 ，托盘，货运单
	    	for (Long id :ids) {
	    		//判断明细是否添加了
				OrderPut orderPut = orderPutService.getById(id);
				if ("PENDING_SUBMIT".equals(orderPut.getBillstate())||"REVOKED".equals(orderPut.getBillstate())) {
					QueryWrapper<OrderPutDetailed> queryWrapper = new QueryWrapper<OrderPutDetailed>();
					queryWrapper.eq("put_detailed_number", orderPut.getPutNumber());
					int count = orderPutDetailedService.count(queryWrapper);
		    		if (count>0) {
		    			data = storagePutSingleService.productionPutSingle(orderPut);
					} else {
						data.setSuccess(false);
						data.setMessage("Please add product details first");
						return data;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();//释放锁
		}
        return data;
    }

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	//查询可选择仓库
    	model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择客户
    	model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/orderPut.html";
    }

    /**
     * 跳转到查询页面list
     */
    @RequestMapping("toQuery")
    public String toQuery(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	//查询可选择仓库
    	model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择客户
    	model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/query.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add(Model model) {
    	prepareViewData(model);
        return PREFIX + "/orderPut_add.html";
    }

    /**
     * 编辑页面
     */
    @RequestMapping("/edit")
    public String edit(@RequestParam("id") Long id ,Model model) {
        OrderPut detail = this.orderPutService.getById(id);
        OrderTransportParam otp = new OrderTransportParam();
        otp.setCustomerOrder(detail.getPutCustomerNumber());
        OrderTransportResult otr = orderTransportService.findBySpec(otp,null);
        model.addAttribute("otr", otr);
    	prepareViewData(model);
        return PREFIX + "/orderPut_edit.html";
    }

    /**
     * 导入页面
     */
    @RequestMapping("/toimportExcel")
    public String toimportExcel(Model model) {
    	prepareViewData(model);
        return PREFIX + "/orderPut_import.html";
    }

    /**
     * 导入页面
     */
    @RequestMapping("/toimportExcelDD")
    public String toimportExcelDD(Model model) {
        prepareViewData(model);
        return PREFIX + "/orderPut_importDD.html";
    }
    /**
     * 准备回显数据
     */
    private void prepareViewData(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	List<DictResult> dictList =  dictService.findByDicttypeCodeAndDictCode3("BUSINESS_CATEGORY","RKZL");//入库种类 字典
    	model.addAttribute("dictList", dictList);
    	List<DictResult> dictListm =  dictService.findByDicttypeCodeAndDictCode3("BUSINESS_CATEGORY","MODES");//出入库方式 字典
    	model.addAttribute("dictListm", dictListm);
    	List<DictResult> dictLists =  dictService.findByDicttypeCode2("SETTLEMENT_METHOD");//结算方式 字典
    	model.addAttribute("dictLists", dictLists);

    	//查询可选择客户
    	model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));

    	//查询可选择仓库
    	model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
    }

    /**
     * 查询客户
     */
    @RequestMapping("/getcustomer")
    @ResponseBody
    public BasicCustomer getcustomer(@RequestParam("putCustomerId") Long putCustomerId) {
    	BasicCustomer basicCustomer = basicCustomerService.getById(putCustomerId);
    	return basicCustomer;
    }

	@Autowired
    private DeptService deptService;

	/**
	 * 获取所属项目单号前缀
	 * @param user
	 * @return
	 */
	public String getOperateIdOrderPrefix(User user) {
		if(user.getOperateId() != null) {
			Dept dept = new Dept();
			dept.setOperateId(user.getOperateId());
			Dept resdept = deptService.findBySpec(dept);
			if(resdept != null) {
				if(ToolUtil.isNotEmpty(resdept.getOrderPrefix())) {
					return resdept.getOrderPrefix();
				} else {
					return user.getAccount().substring(0, 3);
				}
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(OrderPutParam orderPutParam,OrderTransportParam orderTransportParam) {
    	User user = userService.getcurrentUser();
    	ResponseData data = new ResponseData();
    	//校验客户单号 是否存在
    	if (this.orderPutService.checkCustomerOrder(orderPutParam.getPutCustomerNumber())) {
    		data.setSuccess(false);
    		data.setMessage("Customer Order No. in imported data already exists!");
    		return data;
		} else {
    		data.setSuccess(true);
		}

    	Long DeliverId = user.getDeliverId();
    	if (DeliverId!=null) {
    		orderPutParam.setDeliverId(DeliverId);//set 交付id
		}
    	Long deptId = user.getDeptId();
    	if (deptId!=null) {
			orderPutParam.setDeptId(deptId);//set 部门id
		}
		String trayCode = getOperateIdOrderPrefix(user);
    	Object WarehouseId = orderPutParam.getWarehouseId();
    	if (WarehouseId!=null) {
			BasicWarehouse bawBasicWarehouse = basicWarehouseService.getById(Integer.parseInt(WarehouseId.toString()));
			orderPutParam.setWarehouseName(bawBasicWarehouse.getWarehouseName());//set 仓库名称
		}
    	Object CustomerId = orderPutParam.getPutCustomerId();
    	BasicCustomer basicCustomer = new BasicCustomer();
    	if (CustomerId!=null) {
    		basicCustomer = basicCustomerService.getById(Integer.parseInt(CustomerId.toString()));
    		orderPutParam.setPutCustomerName(basicCustomer.getCustomerName());//set 客户名称
    		orderPutParam.setSettlementunit(basicCustomer.getCustomerSettlement());//结算单位
		}
    	orderPutParam.setBillstate("PENDING_SUBMIT");//set 单据状态
    	orderPutParam.setCreateName(user.getName());//set 制单用户名称

    	OrderPut orderPut = this.orderPutService.findByThelastone();
    	if (orderPut!=null) {
    		orderPutParam.setPutNumber(trayCode+"-"+"RK-"+DateUtil.getDateBch()+"-"+(orderPut.getId()+1));
		} else {
			orderPutParam.setPutNumber(trayCode+"-"+"RK-"+DateUtil.getDateBch()+"-"+"1");
		}
        if ("PICKUP".equals(orderPutParam.getPutMode())) {
	        if(basicCustomer != null && ToolUtil.isNotEmpty(basicCustomer.getWmsAddtms())&&basicCustomer.getWmsAddtms().equals("1")) {
	        	if(orderTransportParam!=null){
	        		String getUnit = orderTransportParam.getGetUnit();
	        		String getLocation = orderTransportParam.getGetLocation();
	        		String setUnit = orderTransportParam.getSetUnit();
	        		String setLocation = orderTransportParam.getSetLocation();
	        		//解决参数中的 英文括号
	        		if (getUnit!=null&&getUnit!="") {
	        			getUnit = getUnit.replace("& #40;", "(");
	        			getUnit = getUnit.replace("& #41;", ")");
	        			orderTransportParam.setGetUnit(getUnit);
	        		}
	        		if (getLocation!=null&&getLocation!="") {
	        			getLocation = getLocation.replace("& #40;", "(");
	        			getLocation = getLocation.replace("& #41;", ")");
	        			orderTransportParam.setGetLocation(getLocation);
	        		}
	        		if (setUnit!=null&&setUnit!="") {
	        			setUnit = setUnit.replace("& #40;", "(");
	        			setUnit = setUnit.replace("& #41;", ")");
	        			orderTransportParam.setSetUnit(setUnit);
	        		}
	        		if (setLocation!=null&&setLocation!="") {
	        			setLocation = setLocation.replace("& #40;", "(");
	        			setLocation = setLocation.replace("& #41;", ")");
	        			orderTransportParam.setSetLocation(setLocation);
	        		}
	        	}
	        	orderTransportService.putaddTransport(orderPutParam,orderTransportParam);
	        }
		}
        this.orderPutService.add(orderPutParam);
        return data;
    }

    /**
     * 下载入库订单模板
     * @throws ServletException , IOException
     */
    @RequestMapping("downloadExcelTemplate")
    public void downloadExcelTemplate(HttpServletRequest request,HttpServletResponse response) throws ServletException ,IOException {
        String path = "assets/excel/orderPut.xlsx";
        try {
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
            OutputStream  outputStream = response.getOutputStream();
            //设置下载的文件类型
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //设置下载文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(("Warehouse Order Template " + ".xlsx").getBytes(), "iso-8859-1"));
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
            inputStream.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(OrderPutParam orderPutParam,OrderTransportParam orderTransportParam) {
    	ResponseData data = new ResponseData();
    	//校验客户单号 是否存在
    	OrderPut orderPut = this.orderPutService.getById(orderPutParam.getId());
    	if (!orderPut.getPutCustomerNumber().equals(orderPutParam.getPutCustomerNumber())) {
        	if (this.orderPutService.checkCustomerOrder(orderPutParam.getPutCustomerNumber())) {
        		data.setSuccess(false);
        		data.setMessage("Customer Order No. in imported data already exists!");
        		return data;
    		} else {
        		data.setSuccess(true);
    		}
		}

    	Object WarehouseId = orderPutParam.getWarehouseId();
    	if (WarehouseId!=null) {
			BasicWarehouse bawBasicWarehouse = basicWarehouseService.getById(Integer.parseInt(WarehouseId.toString()));
			orderPutParam.setWarehouseName(bawBasicWarehouse.getWarehouseName());//set 仓库名称
		}
    	Object CustomerId = orderPutParam.getPutCustomerId();
    	BasicCustomer basicCustomer = new BasicCustomer();
    	if (CustomerId!=null) {
    		basicCustomer = basicCustomerService.getById(Integer.parseInt(CustomerId.toString()));
    		orderPutParam.setPutCustomerName(basicCustomer.getCustomerName());//set 客户名称
		}
    	orderPutParam.setUpdateName(userService.getcurrentUser().getName());//set 修改用户名称

        if ("PICKUP".equals(orderPutParam.getPutMode())) {
	        if(basicCustomer != null && ToolUtil.isNotEmpty(basicCustomer.getWmsAddtms())&&basicCustomer.getWmsAddtms().equals("1")) {
	        	if(orderTransportParam!=null){
	        		String getUnit = orderTransportParam.getGetUnit();
	        		String getLocation = orderTransportParam.getGetLocation();
	        		String setUnit = orderTransportParam.getSetUnit();
	        		String setLocation = orderTransportParam.getSetLocation();
	        		//解决参数中的 英文括号
	        		if (getUnit!=null&&getUnit!="") {
	        			getUnit = getUnit.replace("& #40;", "(");
	        			getUnit = getUnit.replace("& #41;", ")");
	        			orderTransportParam.setGetUnit(getUnit);
	        		}
	        		if (getLocation!=null&&getLocation!="") {
	        			getLocation = getLocation.replace("& #40;", "(");
	        			getLocation = getLocation.replace("& #41;", ")");
	        			orderTransportParam.setGetLocation(getLocation);
	        		}
	        		if (setUnit!=null&&setUnit!="") {
	        			setUnit = setUnit.replace("& #40;", "(");
	        			setUnit = setUnit.replace("& #41;", ")");
	        			orderTransportParam.setSetUnit(setUnit);
	        		}
	        		if (setLocation!=null&&setLocation!="") {
	        			setLocation = setLocation.replace("& #40;", "(");
	        			setLocation = setLocation.replace("& #41;", ")");
	        			orderTransportParam.setSetLocation(setLocation);
	        		}
	        	}
	        	orderTransportService.puteditTransport(orderPutParam,orderTransportParam);
	        }
		}
        this.orderPutService.update(orderPutParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(OrderPutParam orderPutParam) {
        this.orderPutService.delete(orderPutParam);
        return ResponseData.success();
    }

    /**
     * 撤回
     */
    @RequestMapping("/revoke")
    @ResponseBody
    public ResponseData revoke(OrderPutParam outOrderParam) {
    	ResponseData data = new ResponseData();
        OrderPut orderPut = orderPutService.getById(outOrderParam.getId());
        if ("PENDING_INBOUND".equals(orderPut.getBillstate())|| "WAREHOUSED".equals(orderPut.getBillstate())) {
        	data = orderPutService.revokeS(orderPut,data);
		} else {
			data.setSuccess(false);
		}
        return data;
    }

    /**
     * 发起
     */
    @RequestMapping("/launch")
    @ResponseBody
    public ResponseData launch(OrderPutParam param) {
    	ResponseData data = new ResponseData();
		OrderPut orderPut = orderPutService.getById(param.getId());
		param.setPutCustomerNumber(orderPut.getPutCustomerNumber());
		OrderPutResult orderPutResult = orderPutService.findBySpec(param,null);
    	if (orderPutResult!=null) {
    		data.setSuccess(false);
    		data.setMessage("Customer Order No. in imported data already exists!");
    		return data;
		}
    	orderPut.setBillstate("PENDING_SUBMIT");
        ToolUtil.copyProperties(orderPut,param);
        orderPutService.update(param);
        return ResponseData.success();
    }

    /**
     * 批量发起
     */
    @RequestMapping("/btnLaunch")
    @ResponseBody
	@Transactional(rollbackFor = Exception.class)
    public ResponseData btnLaunch(@RequestParam("ids") Long[] ids) {
    	ResponseData data = new ResponseData();
    	for (Long id :ids) {
			OrderPut orderPut = orderPutService.getById(id);
			OrderPutParam param = new OrderPutParam();
			param.setPutCustomerNumber(orderPut.getPutCustomerNumber());
			OrderPutResult orderPutResult = orderPutService.findBySpec(param,null);
	    	if (orderPutResult!=null) {
	    		data.setSuccess(false);
	    		data.setMessage("Customer order number already exists");
	    		return data;
			}
			orderPut.setBillstate("PENDING_SUBMIT");
			ToolUtil.copyProperties(orderPut,param);
			orderPutService.update(param);
		}
        return ResponseData.success();
    }

    /**
     * 批量作废
     */
    @RequestMapping("/tovoid")
    @ResponseBody
	@Transactional(rollbackFor = Exception.class)
    public ResponseData tovoid(@RequestParam("ids") Long[] ids) {
    	for (Long id :ids) {
    		try {
    			OrderPut orderPut = orderPutService.getById(id);
    			orderPut.setBillstate("Void");
    			OrderPutParam param = new OrderPutParam();
    	        ToolUtil.copyProperties(orderPut,param);
    			orderPutService.update(param);
			} catch (Exception e) {
				break;
			}
		}
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(OrderPutParam orderPutParam) {
        OrderPut detail = this.orderPutService.getById(orderPutParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(OrderPutParam orderPutParam,String condition ) {
    	User user = userService.getcurrentUser();
    	orderPutParam.setDeliverId(user.getDeliverId());
    	orderPutParam.setDeptId(user.getDeptId());
    	orderPutParam.setBillstate("list");
        return this.orderPutService.findPageBySpec(orderPutParam,condition,user.getOperateId());
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/querylist")
    public LayuiPageInfo querylist(OrderPutParam orderPutParam,String condition,String startTime,String endTime,String startTime2,String endTime2) {
    	User user = userService.getcurrentUser();
    	orderPutParam.setDeliverId(user.getDeliverId());
    	orderPutParam.setDeptId(user.getDeptId());
    	if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
    		orderPutParam.setCreateName("'"+startTime+"' and '"+endTime+"'");
		} else if (startTime == null && endTime == null){
			startTime = DateUtil.getDate();
			endTime = DateUtil.getDate();
			orderPutParam.setCreateName("'"+startTime+"' and '"+endTime+"'");
		}
    	if (ToolUtil.isNotEmpty(startTime2) && ToolUtil.isNotEmpty(endTime2)) {
    		orderPutParam.setCompleteName("'"+startTime2+"' and '"+endTime2+"'");
		}
        return this.orderPutService.querylistPageBySpec(orderPutParam,condition,user.getOperateId());
    }

}


